package one.stand.controller;

import com.github.vioao.wechat.utils.signature.DigestUtil;
import com.github.wxpay.sdk.WXPayUtil;
import com.jmp.base.dto.*;
import com.jmp.feign.AccountFeign;
import com.jmp.feign.SecurityFeign;
import com.jmp.feign.WechatFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import one.stand.advice.ResultEnum;
import one.stand.dto.HmhdLoginRequest;
import one.stand.dto.HmhdLoginResponse;
import one.stand.dto.SignConfigRequest;
import one.stand.dto.SignConfigResponse;
import one.stand.enums.LoginEnum;
import one.stand.model.ResultModel;
import one.stand.service.user.UserAdminService;
import one.stand.util.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(description = "签到管理")
@RestController
@RequestMapping("/hmhd")
@Slf4j
public class HmhdController {
    @Autowired
    private AccountFeign accountFeign;
    @Autowired
    private WechatFeign wechatFeign;
    @Autowired
    private SecurityFeign securityFeign;
    @Autowired
    private UserAdminService userAdminService;

    @ApiOperation("配置")
    @PostMapping("/config")
    public ResultModel<SignConfigResponse> config(@RequestBody SignConfigRequest signConfigRequest) {
        String appid = "wxdf49f5461d18dbc0";
        String appSecret = "2ece02f46e4090c6284291a576daf3df";
        ResultModel<WechatTokenResponse> tokenResponseResultModel = wechatFeign.ticket(WechatTokenRequest.builder().appId(appid).appSecret(appSecret).build());
        AssertUtil.isTrue(tokenResponseResultModel);
        String ticket = tokenResponseResultModel.getData().getToken();
        String noncestr = WXPayUtil.generateNonceStr();
        String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
        String url = signConfigRequest.getUrl();
        String string1 = "jsapi_ticket=" + ticket + "&noncestr=" + noncestr + "&timestamp=" + timestamp + "&url=" + url;
        String signature = DigestUtil.sha1Hex(string1);
        log.info("{} {} {} {}", ticket, noncestr, timestamp, url);
        log.info("{} {}", string1, signature);

        SignConfigResponse response = SignConfigResponse.builder()
                .appId(appid)
                .nonceStr(noncestr)
                .timestamp(timestamp)
                .signature(signature)
                .build();
        return ResultModel.success(response);
    }

    @ApiOperation("登录")
    @PostMapping("/login")
    public ResultModel<HmhdLoginResponse> login(@RequestBody HmhdLoginRequest request) {
        AssertUtil.nonNull(request.getCode(), ResultEnum.PARAM_CHECK, "CODE不能为空");
        SecurityLoginGzhApiRequest wxRequest = SecurityLoginGzhApiRequest.builder()
                .code(request.getCode())
                .loginEnum(LoginEnum.HMHD_GZH)
                .build();
        ResultModel<SecurityLoginGzhApiResponse> result = securityFeign.loginGzhApiWx(wxRequest);
        AssertUtil.isTrue(result);
        HmhdLoginResponse loginResponse = HmhdLoginResponse.builder()
                .avatarUrl(result.getData().getAvatarUrl())
                .auth(!userAdminService.isVisit(result.getData().getAvatarUrl()))
                .cardId(result.getData().getCardId())
                .phone(result.getData().getPhone())
                .token(result.getData().getToken())
                .userId(result.getData().getUserId())
                .userName(result.getData().getUserName())
                .build();
        return ResultModel.success(loginResponse);
    }
}
